/**
 * 列表视图
 */
Ext.define('cc.self.view.SelfJhGridView', {
	extend: 'Ext.grid.Panel',
	uses: ['cc.self.store.SelfJh'],
	requires: ['cc.self.util.SelfService'],
	cls: 'c-grid-header',
	viewModel: {
		data: {
			rec: null,
			selfJh: null
		}
	},
	features: [{
        ftype: 'summary'
	}],
	initComponent: function(){
		var me = this;
		me.title = '结汇';
		me.listeners = {
			//切换viewModel
			selectionchange: function(scope, recs){
				me.getViewModel().set('selfJh', recs[0]);
			}
		},
		me.tbar = [{
			text: '新增',
			iconCls: 'c-btn-create',
			bind: {
				disabled: '{!auth.create}'
			},
			handler: function(){
				var r = Ext.create('cc.self.model.SelfJh', {
					id: 0,
					bus_id: me.getViewModel().get('rec').get('id'),
					jh_date: new Date()
				});

				me.showSelfJhWin('新增', r, true);
			}
		}, {
			text: '编辑',
			iconCls: 'c-btn-update',
			bind: {
				disabled: '{!auth.update}'
			},
			handler: function(){
				var r = me.getViewModel().get('selfJh');
				if(r){
					me.showSelfJhWin('编辑', r, false);
				}
			}
		}, {
			text: '删除',
			iconCls: 'c-btn-delete',
			bind: {
				disabled: '{!auth.delete}'
			},
			handler: function(){
				var r = me.getViewModel().get('selfJh');
				if(r){
					Ext.Msg.confirm('提示', '即将删除该记录，是否继续？', function(btn){
						if(btn == 'yes'){
							CC.Self.deleteSelfJh(r.get('id'), function(){
								me.store.load();
							});
						}
					});
				}
			}
		}];
		if(!me.store){
			me.store = Ext.create('cc.self.store.SelfJh');
			Ext.apply(me.store.proxy.extraParams, {
				bus_id: me.getViewModel().get('rec').get('id')
			});
		}
		me.store.on('datachanged', function(scope){
			var sum = 0;
			var recs = scope.getRange();
			if(recs && recs.length > 0) {
				for(var i = 0; i < recs.length; i++) 
				sum += new Number(Ext.Number.toFixed(recs[i].get('sh_money')*recs[i].get('rate'), 2));
			}
			me.updateJh(sum);
		});
		me.store.on('update', function(scope){
			var sum = 0;
			var recs = scope.getRange();
			if(recs && recs.length > 0) {
				for(var i = 0; i < recs.length; i++) 
				sum += new Number(Ext.Number.toFixed(recs[i].get('sh_money')*recs[i].get('rate'), 2));
			}
			me.updateJh(sum);
		});
		me.store.load();
		
		if(!me.columns){
			me.columns = [{
				xtype: 'rownumberer',
				width: 100,
				text: '序号',
				align: 'center',
				summaryType: 'sum',
		        summaryRenderer: function(value, summaryData, dataIndex) {
		            return '合计：';
		        }
			},{
				xtype: 'datecolumn',
				format: 'Y-m-d',
				text: '结汇日期',
				align: 'center',
				dataIndex: 'jh_date'
			}, {
				text: '收汇金额USD',
				align: 'right',
				xtype: 'numbercolumn', 
				format:'0.00',
				dataIndex: 'sh_money',
				summaryType: 'sum',
				summaryRenderer: function(value, summaryData, dataIndex) {
		            return '$'+value;
		        }
			}, {
				text: '汇率',
				align: 'right',
				xtype: 'numbercolumn', 
				format:'0.00',
				dataIndex: 'rate'
			}, {
				text: '结汇金额RMB',
				align: 'right',
				dataIndex: 'jh_money',
				xtype: 'numbercolumn', 
				format:'0.00',
				renderer: function(v, metaData, rec){
					return Ext.Number.toFixed(rec.get('sh_money') *rec.get('rate'), 2);
				},
				summaryType: 'sum',
				summaryRenderer: function(value, summaryData, dataIndex) {
		            return '￥'+value;
		        }
			}, {
				text: '手续费',
				summaryType: 'sum',
				xtype: 'numbercolumn', 
				format:'0.00',
				align: 'right',
				dataIndex: 'sxf'
			}, {
				text: '兑换汇率',
				align: 'right',
				xtype: 'numbercolumn', 
				format:'0.00',
				dataIndex: 'dh_rate'
			}, {
				text: '金额RMB',
				align: 'right',
				summaryType: 'sum',
				xtype: 'numbercolumn', 
				format:'0.00',
				dataIndex: 'jh_money_rmb'
			}];
		}
		
		me.callParent(arguments);
	},
	//显示实体窗口
	showSelfJhWin: function(title, rec, add){
		var me = this;
		var form = Ext.create('cc.self.view.SelfJh', {
			viewModel: {
				data: {
					rec: rec
				}
			}
		});
		
		var win = Ext.create('bmc.common.comp.EntityWindow', {
			width: 548,
			height: 212,
			title: title,
			form: form,
			cb: function(){
				me.store.load();
			},
			listeners: {
				reject: function(){
					rec.reject();
				}
			}
		});
		win.show();
	}
});
